app.controller('BusRouteListCtrl', function ($scope, ajax, $modal, toaster, $filter, $timeout) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.queryMap = {};
	$scope.stationlist = [];
	$scope.reload = $scope.page.loader('route/bs/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.query = function () { $scope.reload(1, $scope.queryMap); };
	$scope.query();

	// 新增
	$scope.insert = function () {
		$modal.open({
			templateUrl: 'template/bus/route/addModal.html?v=048e54f14f',
			controller: 'BusRouteModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": null,
						"type": 'insert',
						"list": $scope.stationlist
					};
				}
			}
		}).result.then(function (param) {
			inserter.datalist = param.ids;
			inserter.fields = param.data;
			inserter.resource = 'route/bs/insert';
			$scope.page.add(inserter, $scope.reload);
		});
	};

	// 修改
	$scope.update = function (current_data) {
		$scope.current_data = angular.copy(current_data);
		$modal.open({
			templateUrl: 'template/bus/route/modModal.html?v=4aa599e897',
			controller: 'BusRouteModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": 'update',
						"list": $scope.stationlist
					};
				}
			}
		}).result.then(function (param) {
			updater.fields = param.data;
			updater.id = param.data.id;
			updater.resource = 'route/bs/update';
			$scope.page.mod(updater, $scope.reload);
		});
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		$scope.delModal(size, current_data, submit_type).result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'route/bs/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload);
			}
		});
	};

	// function toDistance(distance) {
	// 	if (distance >= 1000) {
	// 		return distance = (Math.round(distance / 100) / 10).toFixed(1);
	// 	} else if (distance < 1000) {
	// 		return distance = distance / 1000;
	// 	}
	// }

	$scope.asc = function (route) {
		ajax.call(new Req.GET({
			resource: 'route/bs/' + route.id + '/station'
		})).then(function (result) {
			$scope.routeStationList = result.result;
			for (var i in $scope.routeStationList.length) {
				// $scope.routeStationList[i].distance = $filter("number")(toDistance($scope.routeStationList[i].distance), "1");
				$scope.routeStationList[i].duration = $filter("number")($scope.routeStationList[i].duration / 60, "1");
			}
			$modal.open({
				templateUrl: 'template/bus/route/sort.html?v=ed72f83a0d',
				controller: 'BusRouteSortCtrl',
				size: 'lg',
				resolve: {
					param: function () {
						return {
							"type": "update",
							"list": $scope.routeStationList,
							"route": route
						};
					}
				}
			}).result.then(function (param) {
				$scope.ids = param;
				updater.datalist = $scope.ids;
				updater.id = route.id;
				updater.resource = 'route/bs/' + route.id + '/orderBy';
				ajax.call(updater).then(function (result) {
					if (result.code == 200) {
						if (result.result.length > 0) $scope.syncShift(route.id, result.result);
						else toaster.pop("success", "成功", "线路排序成功，没有需要同步的发班计划！");
					} else if (result.code != 423) {
						toaster.pop("error", "错误", result.result);
					}
				}, $scope.serverError);
			});
		}, function () { });
	};

	// 同步班次计划
	$scope.syncShift = function (routeId, list) {
		$modal.open({
			templateUrl: 'template/bus/route/syncShift.html?v=9fd825009e',
			controller: 'SyncShiftRouteCtrl',
			size: 'md',
			resolve: {
				param: function () {
					return {
						"type": 'update',
						"list": list,
					};
				}
			}
		}).result.then(function (param) {
			if (!param || param.length <= 0) return;
			ajax.call(new Req.POST({
				datalist: param,
				resource: 'shift/bs/update/route/' + routeId
			})).then(function (result) {
				if (result.code == 200) {
					if (result.result.length > 0) {
						toaster.pop("success", "成功", "同步完成，有部分无法同步的班次！");
						$scope.syncResult(result.result);
					} else {
						toaster.pop("success", "成功", "所选班次同步成功！");
					}
				} else if (result.code != 423) {
					toaster.pop("error", "错误", "同步失败！");
				}
			}, $scope.serverError);
		});
	};

	// 无法同步的班次计划展示
	$scope.syncResult = function (list) {
		$modal.open({
			templateUrl: 'template/bus/route/syncShift.html?v=9fd825009e',
			controller: 'SyncShiftRouteCtrl',
			size: 'md',
			resolve: {
				param: function () {
					return {
						"type": 'show',
						"list": list,
					};
				}
			}
		});
	};

	// 加载所有站点列表
	$timeout(function () {
		ajax.call(new Req.GET({
			resource: 'station/bs/list'
		})).then(function (result) {
			$scope.stationlist = result.result;
		}, function () { });
	}, 100);
});
